/**
 * 
 */   
package com.gson.inter;  

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.IOException;
import java.util.Random;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

/**   
 * @作者 Stone
 * @创建日期 2014-12-1 上午10:50:49
 * @版本 V1.0
 * @类说明
 */
public class XiaoiService {
	private final String app_key = "e5o2EdVKjQWg";
	private final String app_secret = "xuIOV8JXnrgOfLKxmLFY";
	private final String userId = "stone02111";
	
	public String getXiaoiInfo(String key) {
        String realm = "xiaoi.com";
        String method = "POST";
        String uri = "/robot/ask.do";
        byte[] b = new byte[20];
        new Random().nextBytes(b);
        String nonce = new String(Hex.encodeHex(b));
        String HA1 = DigestUtils.shaHex(StringUtils.join(new String[] {
        		app_key, realm, app_secret }, ":"));
        String HA2 = DigestUtils.shaHex(StringUtils.join(new String[] { method,
                uri }, ":"));
        String sign = DigestUtils.shaHex(StringUtils.join(new String[] { HA1,
                nonce, HA2 }, ":"));
 
        String str = null;
 
        HttpClient hc = new HttpClient();
        PostMethod pm = new PostMethod("http://nlp.xiaoi.com/robot/ask.do");
        pm.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
                "utf-8");
        pm.addRequestHeader("X-Auth", "app_key=\"" +app_key+ "\", nonce=\""
                + nonce + "\", signature=\"" + sign + "\"");
        pm.setParameter("platform", "weixin");
        pm.setParameter("type", "0");
        pm.setParameter("userId", userId);
        pm.setParameter("question", key);
        int re_code;
        try {
            re_code = hc.executeMethod(pm);
            if (re_code == 200) {
                str = pm.getResponseBodyAsString();
            }
        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        //替换A标签
        str = str.replace("[/link]", "</a>");
        str = str.replace("[link url='", "<a href=\"");
        str = str.replace("']", "\">");
        
        //去除末尾换行符
        //str = str.replaceAll("\n", "").replaceAll("\r", "");
        for (int i = 0; i < 3; i++) {
        	if(str.endsWith("\r\n")) {
            	str = str.substring(0, str.lastIndexOf("\r\n"));
            }
		}
        
        //替换不知道答案内容
        String noResult = "主人还没给我设置这类话题的回复";
        if(str.startsWith(noResult)) {
        	str = "";
        }
        
        if(StringUtils.isEmpty(str)) {
        	int rand = (int) (Math.random() * 77 % 5);
        	switch (rand) {
			case 0:
				str = "\ue056我不明白你在说什么...";
				break;
			case 1:
				str = "\ue40f你好无聊哦！";
				break;
			case 2:
				str = "\ue105我就不告诉你，就不告诉你...";
				break;
			case 3:
				str = "\ue408今天困了，不和你聊了...";
				break;
			case 4:
				str = "\ue416你再来，我就关门放旺财了！";
				break;
			default:
				str = "\ue416你再来，我就关门放旺财了！";
				break;
			}
        }
        return str;
    }
	
	
    public static void main(String[] args) {
    	XiaoiService t = new XiaoiService();
    	String result = t.getXiaoiInfo("红烧肉做法");
    	System.out.println(result);
    	System.out.println("============");
	}
}
